// pages/cart/cart.js
var app = getApp()
const $api = require('../../utils/api.js').API;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 组件所需的参数
    navbarData: {
      havebg: 1, //1表示不显示    0表示显示
      out: 1, //是否显示左上角图标   1表示显示    0表示不显示
      title: '购物车', //导航栏 中间的标题
      bg: '#fff', //导航背景
    },
    totalPrice: '0.00', //合计价格
    height: app.globalData.height,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log(123)
    let _this = this
    _this.getshoppingcart()
  },
  //列表
  getshoppingcart() {
    let _this = this
    $api.getshoppingcart({
      login_token: wx.getStorageSync('login_token'),
    }).then((res) => {
      if (res.data.code == 0) {
        let goodslist = res.data.data.good_lists
        _this.setData({
          goodslist
        })
      }
    }).catch((err) => {
      _this.setData({
        loadModal: false
      })
    })
  },
  addshoppingcart(data) {
    console.log(data)
    let _this = this
    // return
    $api.addshoppingcart({
      buy_goods: JSON.stringify(data.buy_goods),
      mode: data.is_register,
      type: 2
    }).then((res) => {
      if (res.data.code == 0) {
        var goodslist = _this.data.goodslist;
        goodslist[data.idx].num = data.buy_goods[0].num
        var shopPrice = 0;
        goodslist.forEach((item) => {
          if (item.iconState) {
            shopPrice = shopPrice + parseFloat(item.price) * (parseFloat(item.num));
          }
        })
        shopPrice = shopPrice.toFixed(2)
        _this.setData({ goodslist, totalPrice: shopPrice, })
      }

    }).catch((err) => {
      _this.setData({
        loadModal: false
      })
    })
  },
  //商品数量+1
  onAdd(e) {
    var _this = this;
    var idx = e.currentTarget.dataset.idx;
    var list = _this.data.goodslist;
    let info = {
      buy_goods: [{ good_id: list[idx].good_id, num: parseInt(list[idx].num) + 1 }],
      is_register: list[idx].is_register,
      idx: idx
    }
    _this.addshoppingcart(info)

  },
  //商品数量-1
  onReduce(e) {
    var _this = this;
    var idx = e.currentTarget.dataset.idx;
    var list = _this.data.goodslist;

    if (parseInt(list[idx].num) - 1 <= 1) {
      _this.setData({
        ['list[' + idx + '].num']: 1
      })
    } else {
      let info = {
        buy_goods: [{ good_id: list[idx].good_id, num: parseInt(list[idx].num) - 1 }],
        is_register: list[idx].is_register,
        idx: idx
      }
      _this.addshoppingcart(info)
    }


  },
  //选择商品
  onChoiceShop(e) {
    var _this = this;
    var idx = e.currentTarget.dataset.idx;
    var goodslist = _this.data.goodslist;
    var shopPrice = 0;
    if (goodslist[idx].iconState) {
      _this.setData({
        ['goodslist[' + idx + '].iconState']: false
      })
    } else {
      _this.setData({
        ['goodslist[' + idx + '].iconState']: true
      })
    }
    let is = 0
    for (var i = 0; i < goodslist.length; i++) {
      if (goodslist[i].iconState) {
        is++
        shopPrice = shopPrice + parseFloat(goodslist[i].price) * parseFloat(goodslist[i].num);
      }
    }
    console.log(shopPrice)
    shopPrice = shopPrice.toFixed(2)
    _this.setData({
      totalPrice: shopPrice,
    })
    let QxiconState = is == goodslist.length ? true : false
    _this.setData({
      QxiconState,
      deleteis: is
    })
  },
  onqx() {
    let _this = this
    var goodslist = _this.data.goodslist;
    var shopPrice = 0;
    var QxiconState = _this.data.QxiconState
    goodslist.forEach((item) => {
      item.iconState = QxiconState ? false : true
      if (!QxiconState) {
        shopPrice = shopPrice + parseFloat(item.price) * parseFloat(item.num);
      }
    })
    shopPrice = shopPrice.toFixed(2)
    _this.setData({
      totalPrice: shopPrice,
      QxiconState: QxiconState ? false : true,
      goodslist
    })
  },
  ondelete() {
    let _this = this
    wx.showModal({
      title: '提示',
      content: '确定要删除选中商品吗？',
      success(res) {
        if (res.confirm) {
          var goodslist = _this.data.goodslist;
          let list = []
          goodslist.forEach((item) => {
            if (item.iconState) {
              list.push(item.good_id)
            }
          })
          $api.deleteshoppingcart({
            buy_goods: JSON.stringify(list),
          }).then((res) => {
            if (res.data.code == 0) {
              wx.showToast({
                title: res.data.msg,
                icon: 'none',
                duration: 2000
              })
              _this.setData({
                totalPrice: 0,
                QxiconState: false,
              })
              _this.getshoppingcart()
            } else {
              wx.showToast({
                title: res.data.msg,
                icon: 'none',
                duration: 2000
              })
            }

          }).catch((err) => {
            _this.setData({
              loadModal: false
            })
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  onReading() {
    let _this = this
    wx.removeStorageSync('address')
    wx.removeStorageSync('applyShop')
    wx.removeStorageSync('wait_pay')
    wx.removeStorageSync('giftId')
    wx.removeStorageSync('pay')
    wx.removeStorageSync('goodsList')
    wx.removeStorageSync('addressid')
    let list = []
    let goodslist = _this.data.goodslist
    let i0 = 0
    let i2 = 0
    let is_register = ''
    goodslist.forEach((item) => {
      if (item.iconState) {
        item.id = item.good_id
        list.push(item)
        is_register = item.is_register
        if (item.is_register == 0) {
          i0++
        }
        if (item.is_register == 2) {
          i2++
        }
      }
    })
    if (i0 && i2) {
      wx.showToast({
        title: '请选择同一商品类型下单',
        icon: 'none',
        duration: 2000
      })
      return
    }
    list = JSON.stringify(list)
    wx.navigateTo({
      url: '/pages/reading/reading?state=3&&twoType=1&&ptType=1&&goods=' + list + '&is_register=' + is_register,
    })
  }
})